package cn.zhz.service;

import cn.zhz.dao.DeptDao;
import cn.zhz.dao.EmpDao;
import cn.zhz.pojo.Dept;
import cn.zhz.pojo.Emp;
import cn.zhz.pojo.EmpCondition;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import java.util.Iterator;
import java.util.List;

public class EmpService {
    private EmpDao dao = new EmpDao();

    public Iterator<Emp> findIterate() {
        Iterator<Emp> iterator = null;
        Transaction tx = null;

        try {
            tx = dao.currentSession().beginTransaction();

            iterator = dao.findIterae();
            //遍历并输出结果，与调用list()方法时不同，须在会话关闭前测试查询效果
            Emp emp = null;
            while (iterator.hasNext()) {
                emp = iterator.next();
                System.out.println(emp.getEname());
            }

            tx.commit();
        } catch (HibernateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
        return iterator;
    }

    public List<Emp> findEmpsByConditions(Object[] conditions) {
        Transaction tx = null;
        List<Emp> emps = null;
        try {
            tx = dao.currentSession().beginTransaction();
            emps = dao.findByConditions(conditions);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
        return emps;
    }

    public List<Emp> findEmpsByConditions(Emp conditions) {
        Transaction tx = null;
        List<Emp> emps = null;
        try {
            tx = dao.currentSession().beginTransaction(); // (1)开始事务
            emps = dao.findByConditions(conditions); // (2)持久化操作
            tx.commit(); // (3)提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // (4)回滚事务
        }
        return emps;
    }

    public void save(Emp emp) {
        Transaction tx = null;

        try {
            tx = dao.currentSession().beginTransaction();

            dao.save(emp);

            tx.commit();
        } catch (HibernateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
    }

    public List<Emp> findByInvisibleJoin(String deptName) {
        Transaction tx = null;
        List<Emp> emps = null;
        try {
            tx = dao.currentSession().beginTransaction(); // 开启事务
            emps = new EmpDao().findByInvisibleJoin(deptName);
            tx.commit(); // 提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // 回滚事务
        }
        return emps;
    }

    public List<Object[]> findGroupBy() {
        Transaction tx = null;
        List<Object[]> result = null;
        try {
            tx = dao.currentSession().beginTransaction(); // 开启事务
            result = dao.findGroupBy();
            tx.commit(); // 提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // 回滚事务
        }
        return result;
    }

    public List<Emp> findEmpsByCOnditions2(Emp conditions) {
        Transaction tx = null;
        List<Emp> emps = null;
        try {
            tx = dao.currentSession().beginTransaction();
            emps = dao.findByConditions(conditions);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
        return emps;
    }

    public List<Emp> findEmpsByConditions(EmpCondition condition) {
        Transaction tx = null;
        List<Emp> emps = null;
        try {
            tx=dao.currentSession().beginTransaction();
            StringBuilder hql = new StringBuilder("from Emp as emp where 1=1 ");
            if(condition.getJob()!=null&&condition.getJob().length()>0){
                hql.append(" and emp.job = :job");
            }
            if(condition.getSalary()!=null && condition.getSalary()!=0){
                hql.append(" and emp.sal > :salary");
            }
            if(null!=condition.getHireDateStart()){
                hql.append(" and emp.hiredate > :hireDateStart");
            }
            if(null!=condition.getHireDateEnd()){
                hql.append(" and emp.hiredate < :hireDateEnd");
            }
            emps = new EmpDao().findByConditions(hql.toString(),condition);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx != null)
                tx.rollback();
        }
        return emps;
    }
    public Long countBySalary(Double sal){
        Transaction tx =null;
        Long result = null;
        try {
            tx=dao.currentSession().beginTransaction();
            result=dao.obtainTheRowCount(sal);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx!=null)
                tx.rollback();
        }
        return result;
    }

    public List<Emp> findEmpsByPage(int pageNo,int pageSize){
        Transaction tx =null;
        List<Emp> emps = null;
        try {
            tx=dao.currentSession().beginTransaction();
            emps=new EmpDao().findByPage(pageNo,pageSize);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx!= null)
                tx.rollback();
        }
        return emps;
    }

    public void addNewEmp(Emp emp){
        Transaction tx = null;
        try {
            tx=dao.currentSession().beginTransaction();
            dao.save(emp);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null)
                tx.rollback();
        }
    }
    public List<Emp> findEmpsByDept(Dept dept){
        Transaction tx = null;
        List<Emp> result = null;
        try {
            tx=dao.currentSession().beginTransaction();
            result=dao.findByDept(dept);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null)
                tx.rollback();
        }
        return  result;
    }
    public List<Emp> findAllEmps(){
        Transaction tx = null;
        List<Emp> empList = null;
        try {
            tx=dao.currentSession().beginTransaction();
            empList = dao.findAll();
            for (Emp emp :empList){
                System.out.println("员工姓名："+emp.getEname()+"\t");
                System.out.println("所在部门："+emp.getDept().getDeptName());
            }
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null)
                tx.rollback();
        }
        return empList;
    }
    public void changeDept(Integer empNo,Integer deptNo){
        Transaction tx = null;
        try {
            tx=dao.currentSession().beginTransaction();
            Dept dept = new DeptDao().load(deptNo);
            Emp emp = dao.load(empNo);
            emp.setDept(dept);
            dept.getEmps().add(emp);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null)
                tx.rollback();
        }
    }
}
